﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using MemberShipDAL.DataObject;
using MemberServerBLL;


public partial class ControlPanel_Role_RoleTree : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {


            //把所有Roles取出
            List<RoleData> userInRole = RoleDataObject.GetRoles();

            //如果没有配置权限组，那么树就不显示
            if (userInRole.Count == 0)
            {
                Response.Write("<br/><br/><font color=red>您还没有添加角色，请到角色管理里面添加</font>");
                Response.End();
            }

            DataResources.wmf_ResourcesGroupDataTable dt = new BLL_ResourcesGroup().GetAllData();

            //如果没有配置权限组，那么树就不显示
            if (dt.Rows.Count == 0)
            {
                Response.Write("<br/><br/><font color=red>您还没有配置权限组的基本数据，请运行App_Data下的BaseData.Sql</font>");
                Response.End();
            }
            foreach (RoleData role in userInRole)
            {
                TreeNode tn = new TreeNode(role.RoleName, role.RoleId);
                tn.ToolTip = "角色";
                //tn.NavigateUrl = "javascript:void(0)";
                ResourcesTreeBind("", null, dt, tn, role.RoleId, role.RoleName);
                trv_RoleTree.Nodes.Add(tn);
            }
        }
    }

    /// <summary>
    /// 绑定资源树，递归算法
    /// </summary>
    /// <param name="trv_Resources"></param>
    public bool ResourcesTreeBind(string parentId, TreeNode pNode, DataTable dsResources, TreeNode trv_Resources, string roleId, string roleName)
    {
        DataView dvTree = new DataView(dsResources);

        if (parentId != "")
        {
            dvTree.RowFilter = "[ParentId] ='" + parentId + "'";
        }
        else
        {
            dvTree.RowFilter = "[ParentId] is null";
        }

        foreach (DataRowView row in dvTree)
        {
            TreeNode tn = new TreeNode(row["ResourcesGroupName"].ToString());
            //tn.Value = row["ResourceGroupId"].ToString();
            tn.NavigateUrl = "Javascript:Go(\"" + "Privilege.aspx?Id=" + roleId + "&Name=" + roleName + "&GId=" + row["ResourceGroupId"].ToString() + "&GName=" + HttpUtility.UrlEncodeUnicode(tn.Value) + "\")";


            if (pNode == null)
            {
                trv_Resources.ChildNodes.Add(tn);
                trv_Resources.Expanded = false;
                ResourcesTreeBind(row["ResourceGroupId"].ToString(), tn, dsResources, trv_Resources, roleId, roleName);
            }
            else
            {
                pNode.ChildNodes.Add(tn);
                ResourcesTreeBind(row["ResourceGroupId"].ToString(), tn, dsResources, trv_Resources, roleId, roleName);
            }
        }
        return true;
    }

    /// <summary>
    /// 此方法暂时不用
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void trv_RoleTree_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (trv_RoleTree.SelectedNode.ToolTip == "角色")
        {
            trv_RoleTree.CollapseAll();
            trv_RoleTree.SelectedNode.ExpandAll();
        }
    }
}
